﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace MyLibrary.Updater
{
    public delegate void UpdateProcessHandler();

    public class AutoUpdaterConfig
    {
        private bool enable = true;
        public bool Enable
        {
            get { return enable; }
            set { enable = value; }
        }

        private string serverUrl = String.Empty;
        public string ServerURL
        {
            get { return serverUrl; }
            set { serverUrl = value; }
        }

        UpdateFileList updateFileList = new UpdateFileList();
        public UpdateFileList UpdateFileList
        {
            get { return updateFileList; }
            set { updateFileList = value; }
        }

        public static AutoUpdaterConfig LoadConfig(string file)
        {
            XmlSerializer xs = new XmlSerializer(typeof(AutoUpdaterConfig));
            StreamReader sr = new StreamReader(file);
            AutoUpdaterConfig config = xs.Deserialize(sr) as AutoUpdaterConfig;
            sr.Close();
            return config;
        }

        public void SaveConfig(string file)
        {
            this.ServerURL = LoadConfig(file).ServerURL;
            XmlSerializer xs = new XmlSerializer(typeof(AutoUpdaterConfig));
            StreamWriter sw = new StreamWriter(file);
            xs.Serialize(sw, this);
            sw.Close();
        }
    }

    public class LocalFileInfo
    {
        private string path = "";
        private string lastVer = "";
        private long size = 0;

        [XmlAttribute("path")]
        public string Path
        {
            get { return path; }
            set { path = value; }
        }

        [XmlAttribute("lastversion")]
        public string LastVersion
        {
            get { return lastVer; }
            set { lastVer = value; }
        }

        [XmlAttribute("size")]
        public long Size
        {
            get { return size; }
            set { size = value; }
        }

        public LocalFileInfo(string path, string ver, long size)
        {
            this.path = path;
            this.lastVer = ver;
            this.size = size;
        }

        public LocalFileInfo() { }
    }

    public class RemoteFileInfo
    {
        private string id = String.Empty;
        private string path = String.Empty;
        private string url = String.Empty;
        private string lastVersion = String.Empty;
        private long size = 0;
        private bool needRestart = false;
        private bool isMandatoryUpdate = false;  //强制更新 

        public string FileID { get { return id; } }
        public string Path { get { return path; } }
        public string Url { get { return url; } }
        public string LastVer { get { return lastVersion; } }
        public long Size { get { return size; } }
        public bool NeedRestart { get { return needRestart; } }
        public bool IsMandatoryUpdate { get { return isMandatoryUpdate; } }

        public RemoteFileInfo(XmlNode node)
        {
            this.id = node.Attributes["id"].Value;
            this.path = node.Attributes["path"].Value;
            this.url = node.Attributes["url"].Value;
            if (url.IndexOf("?FileID=") == -1)
            {
                url += "?FileID=";
                url += id;
            }
            this.lastVersion = node.Attributes["lastversion"].Value;
            this.size = Convert.ToInt32(node.Attributes["size"].Value);
            this.needRestart = Convert.ToBoolean(node.Attributes["needrestart"].Value);
            this.isMandatoryUpdate = Convert.ToBoolean(node.Attributes["ismandatoryupdate"].Value);
        }
    }

    public class DownloadFileInfo
    {
        string downloadUrl = String.Empty;
        string fileName = String.Empty;
        string lastVer = String.Empty;
        long size = 0;

        public string DownloadURL
        {
            get { return downloadUrl; }
        }

        public string FileFullName
        {
            get { return fileName; }
        }

        public string FileName
        {
            get { return Path.GetFileName(FileFullName); }
        }

        public string LastVersion
        {
            get { return lastVer; }
            set { lastVer = value; }
        }

        public long Size
        {
            get { return size; }
        }

        public DownloadFileInfo(string url, string name, string ver, long size)
        {
            this.downloadUrl = url;
            this.fileName = name;
            this.lastVer = ver;
            this.size = size;
        }
    }

    public class UpdateFileList : List<LocalFileInfo>
    {
    }
}
